const {ccclass} = cc._decorator;

@ccclass



export class UserMgr extends cc.Component{
    account;
    userId;
    userName;
    lv = 0;
    exp = 0;
    conis = 0;
    sign = 0;
    ip = '';
    sex = 0;
    roomData;
    oldRoomId;
    gems;

    notice;
    gemstip;

    onAuth(ret:cc.ret){
        const self = cc.vv.userMgr;
        if(ret.errcode !== 0){
            console.log(ret.errmsg);
        }
        else{
            self.account = ret.account;
            self.sign = ret.sign;
            cc.vv.http.url = 'http://' + cc.vv.SI.hall;
            self.login();
        }
    }

    guestAuth(){
        let account = cc.args['account'];
        if(account == null){
            account = cc.sys.localStorage.getItem('account');
        }

        if(account == null){
            account = Date.now();
            cc.sys.localStorage.setItem('account', account);
        }

        cc.vv.http.sendRequest('/guest', {account: account}, this.onAuth);
    }

    login(){
        const onLogin = (ret:cc.ret) => {
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                if(!ret.userId){
                    console.log('%c切换到创建角色界面！', 'background: green; color:red;font-size:50px;');
                    cc.director.loadScene('createrole');
                }
                else{
                    this.account = ret.account;
                    this.userId = ret.userId;
                    this.userName = ret.name;
                    this.lv = ret.lv;
                    this.exp = ret.exp;
                    this.conis = ret.coins;
                    this.gems = ret.gems;
                    this.roomData = ret.roomId;
                    this.sex = ret.sex;
                    this.ip = ret.ip;
                    console.log('%c切换到大厅界面！', 'background: green; color:red;font-size:50px;');
                    cc.director.loadScene('hall');
                }
            }
        };

        cc.vv.wc.show('正在登录游戏');
        cc.vv.http.sendRequest('/login', {account: this.account, sign: this.sign}, onLogin);
    }


    /**
     * 创建用户并登陆
     */
    create(name: string){

        const onCreate = (ret:cc.ret) => {
            if(ret.errcode == 0){
                this.login();
            }
            else{
                console.log('创建用户出错：  ', ret.errmsg);
            }
        };
        const data = <cc.ret>{
            account: this.account,
            sign: this.sign,
            name: name
        };
        cc.vv.http.sendRequest('/create_user', data, onCreate);
    }

    /**
     * 进入房间请求
     */
    entryRoom(roomId, callback?){
        const onEntry = (ret: cc.ret) => {
            if(ret.errcode !== 0){
                if(ret.errcode == -1){
                    setTimeout(() => {
                        this.entryRoom(roomId, callback);
                    }, 5000);
                }
                else{
                    cc.vv.wc.hide();
                    if(callback != null){
                        callback(ret);
                    }
                }
            }
            else{
                cc.vv.wc.hide();
                if(callback != null){
                    callback(ret);
                }
                cc.vv.gameNetMgr.connectGameServer(ret);
            }
        };
        const data: cc.ret = {
            account: cc.vv.userMgr.account,
            sign: cc.vv.userMgr.sign,
            roomId: roomId,
        };
        cc.vv.wc.show('正在进入房间 ' + roomId);
        cc.vv.http.sendRequest('/enter_private_room', data, onEntry);
    }

    /**
     * 请求历史记录
     */
    getHistoryList(callback?:(data:cc.history[]) => void){

        const onGet = (ret: cc.ret) => {
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                if(callback != null){
                    callback(ret.history);
                }
            }
        };

        const data: cc.ret = {
            account: cc.vv.userMgr.account,
            sign: cc.vv.userMgr.sign,
        };

        cc.vv.http.sendRequest('/get_history_list', data, onGet);
    }


    getGamesOfRoom(uuid, callback:(data: cc.roomInfo[]) => void){
        const onGet = (ret: cc.ret) => {
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                callback(ret.data);
            }
        };

        const data: cc.ret = {
            account: cc.vv.userMgr.account,
            sign: cc.vv.userMgr.sign,
            uuid: uuid,
        };
        cc.vv.http.sendRequest('/get_games_of_room', data, onGet)
    }

    getDetailOfGame(uuid, index, callback:(data: cc.roomInfo) => void){

        const onGet = (ret: cc.ret) => {
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                callback(ret.data);
            }
        };

        const data: cc.ret = {
            account: cc.vv.userMgr.account,
            sign: cc.vv.userMgr.sign,
            uuid: uuid,
            index: index,
        };
        cc.vv.http.sendRequest('/get_detail_of_game', data,  onGet);
    }
}